package cn.zhou.web.controller;

import cn.zhou.domain.User;
import cn.zhou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zy9524
 * @date 2019/6/9 19:26
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        System.out.println("=======================================");
       User user = userService.findUserByUsernameAndPassword(username,password);
       if (user!=null){
           session.setAttribute("user",user);
           return "redirect:/index.jsp";
       }
        return "redirect:/index.jsp";
    }

    @RequestMapping("/findAllUser")
    public ModelAndView findAllUser(ModelAndView modelAndView){
        List<User> userList = userService.findAllUser();
        System.out.println(userList);
        modelAndView.addObject("userList", userList);
        modelAndView.setViewName("userList");
        return modelAndView;
    }

}
